import { deleteDisease } from './medicalCondition'
import { request, HttpMethod, restartTimes, RequestConfig } from './request'

export const bindDevice = (data:object) => {
  const config = <RequestConfig>{
    url: '/patient/patientDevice/add_device',
    method: HttpMethod.POST,
    data: data
  }
  return request(config);
}

export const bindXinyongDevice = (data: object) => {
  const config = <RequestConfig>{
    url: '/patient/patientDeviceV2/bind_device',
    method: HttpMethod.POST,
    data: data
  }
  return request(config);
}

export const unbindXinyongDevice = (data: object) => {
  const config = <RequestConfig>{
    url: '/patient/patientDeviceV2/unbind_device',
    method: HttpMethod.POST,
    data: data
  }
  return request(config);
}

export const unbindDevice = (data:object) => {
  const config = <RequestConfig>{
    url: '/patient/patientDevice/unbind_device',
    method: HttpMethod.POST,
    data: data
  }
  return request(config);
}

export const getXinyongTokenApi = (patientUid: string | number) => {
  const config = <RequestConfig>{
    url: `/patient/patientDeviceV2/refresh_token/${patientUid}`,
    method: HttpMethod.GET,
  }
  return request(config);
}

export const syncDeviceDataApi = (data:object) => {
  const config = <RequestConfig>{
    url: '/patient/patientDevice',
    method: HttpMethod.POST,
    data: data
  }
  return request(config);
}

export const getDeviceDataApi = (data: object) => {
  const config = <RequestConfig>{
    url: '/patient/patientDevice/getByPatientDeviceObject',
    method: HttpMethod.POST,
    data: data
  }
  return request(config);
}

// 更新心永设备信息（身高、体重）
export const updateXinyongDevice = (data: object) => {
  const config = <RequestConfig>{
    url: '/patient/patientDeviceV2/update_device',
    method: HttpMethod.POST,
    data: data
  }
  return request(config);
}

// 获取心永设备信息
export const getXinyongDeviceByUid = (uid: string | number) => {
  const config = <RequestConfig>{
    url: `/patient/patientDeviceV2/${uid}`,
    method: HttpMethod.GET,
  }
  return request(config);
}

// ============ 血压自动测量配置相关接口 ============

/**
 * 获取用户设备访问 token
 * @param uid 患者 patientUid
 * @returns user_access_token (有效期2小时)
 */
export const getUserAccessToken = (uid: string | number) => {
  const config = <RequestConfig>{
    url: `/patient/userDevice2Config/userAccessToken/${uid}`,
    method: HttpMethod.GET,
    data: { uid }
  }
  return request(config);
}

/**
 * 开启/关闭自动测量总开关
 * @param enable true 开启，false 关闭
 * @param intervalSeconds 默认间隔秒数，未传默认 1800
 * @param userAccessToken 用户 access token
 */
export const switchAutoMeasure = (data: {
  enable: boolean;
  intervalSeconds?: number;
  user_access_token: string;
}) => {
  const config = <RequestConfig>{
    url: `/patient/userDevice2Config/switchAutoMeasure`,
    method: HttpMethod.POST,
    data: data,
    header: {
      'user_access_token': data.user_access_token
    }
  }
  return request(config);
}

/**
 * 设置白天时间段
 * @param startHour 白天开始整点，范围 0-23，示例：6
 * @param endHour 白天结束整点，范围 0-23，示例：22
 * @param userAccessToken 用户 access token
 */
export const setDaytimeRange = (data: {
  startHour: number;
  endHour: number;
  user_access_token: string;
}) => {
  const config = <RequestConfig>{
    url: `/patient/userDevice2Config/setDaytimeRange`,
    method: HttpMethod.POST,
    data: data,
    header: {
      'user_access_token': data.user_access_token
    }
  }
  return request(config);
}

/**
 * 设置白天静默测量间隔
 * @param intervalSeconds 白天静默测量间隔（秒），示例：1800
 * @param userAccessToken 用户 access token
 */
export const setDaytimeInterval = (data: {
  intervalSeconds: number;
  user_access_token: string;
}) => {
  const config = <RequestConfig>{
    url: `/patient/userDevice2Config/setDaytimeInterval`,
    method: HttpMethod.POST,
    data: data,
    header: {
      'user_access_token': data.user_access_token
    }
  }
  return request(config);
}

/**
 * 设置夜间静默测量间隔
 * @param intervalSeconds 夜间静默测量间隔（秒），示例：1800
 * @param userAccessToken 用户 access token
 */
export const setNightInterval = (data: {
  intervalSeconds: number;
  user_access_token: string;
}) => {
  const config = <RequestConfig>{
    url: `/patient/userDevice2Config/setNightInterval`,
    method: HttpMethod.POST,
    data: data,
    header: {
      'user_access_token': data.user_access_token
    }
  }
  return request(config);
}

/**
 * 查询个人配置列表
 * @param userAccessToken 用户 access token
 * @returns 当前用户的所有配置项（key/value/property）
 */
export const listUserConfigs = (userAccessToken: string) => {
  const config = <RequestConfig>{
    url: `/patient/userDevice2Config/listUserConfigs`,
    method: HttpMethod.POST,
    data: {
      user_access_token: userAccessToken
    },
    header: {
      'user_access_token': userAccessToken
    }
  }
  return request(config);
}

/**
 * 设置静默测量模式
 * @param enable 是否开启
 * @param method 0=脉搏波 PPG, 1=示波法
 * @param userAccessToken 用户 access token
 */
export const setSilentMode = (data: {
  enable: boolean;
  method: number;
  user_access_token: string;
}) => {
  const config = <RequestConfig>{
    url: `/patient/userDevice2Config/setSilentMode`,
    method: HttpMethod.POST,
    data: data,
    header: {
      'user_access_token': data.user_access_token
    }
  }
  return request(config);
}

/**
 * 设置白天是否加压
 * @param pressurize true=加压, false=不加压
 * @param userAccessToken 用户 access token
 */
export const setDaytimePressurize = (data: {
  pressurize: boolean;
  user_access_token: string;
}) => {
  const config = <RequestConfig>{
    url: `/patient/userDevice2Config/setDaytimePressurize`,
    method: HttpMethod.POST,
    data: data,
    header: {
      'user_access_token': data.user_access_token
    }
  }
  return request(config);
}

/**
 * 设置夜间是否加压
 * @param pressurize true=加压, false=不加压
 * @param userAccessToken 用户 access token
 */
export const setNightPressurize = (data: {
  pressurize: boolean;
  user_access_token: string;
}) => {
  const config = <RequestConfig>{
    url: `/patient/userDevice2Config/setNightPressurize`,
    method: HttpMethod.POST,
    data: data,
    header: {
      'user_access_token': data.user_access_token
    }
  }
  return request(config);
}